using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DBUtility;
using System.Text;
using System.Data;

namespace BLL
{
    public class Promotion
    {
        public static SqlDataReader GetRLJE(decimal amt)
        {
            string time = DateTime.Now.ToString();
            SqlDataReader myReader = SqlHelper.ExecuteReader(SqlHelper.LocalSqlServer,
                                    "select top 1 * from CXFAD7 where BYZD1=0 and BYZD2=4 and BYZD5<=" + amt.ToString() + " and  '" + time + "'>=RQ_S and '" + time + "'<=RQ_E order by BYZD5 desc");
            return myReader;
        }

        public static SqlDataReader GetCouponAmount(decimal amt)
        {
            string time = DateTime.Now.ToString();
            SqlDataReader myReader = SqlHelper.ExecuteReader(SqlHelper.LocalSqlServer,
                                    "select top 1 * from CXFAD7 where BYZD1=1 and BYZD2=4 and BYZD5<=" + amt.ToString() + " and  '" + time + "'>=RQ_S and '" + time + "'<=RQ_E order by BYZD5 desc");
            return myReader;
        }

        public static void BuildInitCouponSqlString(decimal amt, string orderId, string customerCode, StringBuilder strSqlToUpdateBStyleDB, StringBuilder strSqlToUpdateErpDB)
        {
            decimal couponAmount = 0.00M;
            SqlDataReader dataReader = GetCouponAmount(amt);
            if (dataReader.HasRows)
            {
                if (dataReader.Read())
                {
                    couponAmount = Convert.ToDecimal(dataReader["BYZD6"]);
                    dataReader.Close();
                    dataReader.Dispose();

                    DataSet dst = new CZKZCDMX().GetCouponCodeByAmount(couponAmount, DateTime.Now);
                    if (dst.Tables[0].Rows.Count > 0)
                    {
                        StringBuilder sbSql = new StringBuilder();

                        strSqlToUpdateBStyleDB.Append("update czkzcdmx set state=3 where DJBH='");
                        strSqlToUpdateBStyleDB.Append(dst.Tables[0].Rows[0]["DJBH"].ToString());
                        strSqlToUpdateBStyleDB.Append("' AND CZKBH='");
                        strSqlToUpdateBStyleDB.Append(dst.Tables[0].Rows[0]["CZKBH"].ToString());
                        strSqlToUpdateBStyleDB.Append("'|");

                        strSqlToUpdateErpDB.Append("update czkzcdmx set state=3 where DJBH='");
                        strSqlToUpdateErpDB.Append(dst.Tables[0].Rows[0]["DJBH"].ToString());
                        strSqlToUpdateErpDB.Append("' AND CZKBH='");
                        strSqlToUpdateErpDB.Append(dst.Tables[0].Rows[0]["CZKBH"].ToString());
                        strSqlToUpdateErpDB.Append("'|");


                        strSqlToUpdateBStyleDB.Append("insert into LIQUAN_CUSTOMER(CouponCode,CustomerCode,Amount,OrderCode,Active,ActiveDate,Frozen) values('");
                        strSqlToUpdateBStyleDB.Append(dst.Tables[0].Rows[0]["CZKBH"].ToString());
                        strSqlToUpdateBStyleDB.Append("','");
                        strSqlToUpdateBStyleDB.Append(customerCode);
                        strSqlToUpdateBStyleDB.Append("',");
                        strSqlToUpdateBStyleDB.Append(couponAmount);
                        strSqlToUpdateBStyleDB.Append(",'");
                        strSqlToUpdateBStyleDB.Append(orderId);
                        strSqlToUpdateBStyleDB.Append("',1,'");
                        strSqlToUpdateBStyleDB.Append(DateTime.Now);
                        strSqlToUpdateBStyleDB.Append("',0)|");

                    }
                    else
                    {
                        strSqlToUpdateBStyleDB.Append("");
                        strSqlToUpdateErpDB.Append("");
                    }
                }
                else
                {
                    strSqlToUpdateBStyleDB.Append("");
                    strSqlToUpdateErpDB.Append("");
                }
            }
            else
            {
                strSqlToUpdateBStyleDB.Append("");
                strSqlToUpdateErpDB.Append("");
            }
        }


        public static void BuildInitCouponSqlString(decimal amt, string orderId, string customerCode, out StringBuilder strSqlToUpdateBStyleDB, out StringBuilder strSqlToUpdateErpDB)
        {
            strSqlToUpdateBStyleDB = new StringBuilder();
            strSqlToUpdateErpDB = new StringBuilder();

            decimal couponAmount = 0.00M;
            SqlDataReader dataReader = GetCouponAmount(amt);
            if (dataReader.HasRows)
            {
                if (dataReader.Read())
                {
                    couponAmount = Convert.ToDecimal(dataReader["BYZD6"]);
                    dataReader.Close();
                    dataReader.Dispose();

                    DataSet dst = new CZKZCDMX().GetCouponCodeByAmount(couponAmount, DateTime.Now);
                    if (dst.Tables[0].Rows.Count > 0)
                    {
                        StringBuilder sbSql = new StringBuilder();

                        strSqlToUpdateBStyleDB.Append("update czkzcdmx set state=3 where DJBH='");
                        strSqlToUpdateBStyleDB.Append(dst.Tables[0].Rows[0]["DJBH"].ToString());
                        strSqlToUpdateBStyleDB.Append("' AND CZKBH='");
                        strSqlToUpdateBStyleDB.Append(dst.Tables[0].Rows[0]["CZKBH"].ToString());
                        strSqlToUpdateBStyleDB.Append("'|");

                        strSqlToUpdateErpDB.Append("update czkzcdmx set state=3 where DJBH='");
                        strSqlToUpdateErpDB.Append(dst.Tables[0].Rows[0]["DJBH"].ToString());
                        strSqlToUpdateErpDB.Append("' AND CZKBH='");
                        strSqlToUpdateErpDB.Append(dst.Tables[0].Rows[0]["CZKBH"].ToString());
                        strSqlToUpdateErpDB.Append("'|");


                        strSqlToUpdateBStyleDB.Append("insert into LIQUAN_CUSTOMER(CouponCode,CustomerCode,Amount,OrderCode,Active,ActiveDate,Frozen) values('");
                        strSqlToUpdateBStyleDB.Append(dst.Tables[0].Rows[0]["CZKBH"].ToString());
                        strSqlToUpdateBStyleDB.Append("','");
                        strSqlToUpdateBStyleDB.Append(customerCode);
                        strSqlToUpdateBStyleDB.Append("',");
                        strSqlToUpdateBStyleDB.Append(couponAmount);
                        strSqlToUpdateBStyleDB.Append(",'");
                        strSqlToUpdateBStyleDB.Append(orderId);
                        strSqlToUpdateBStyleDB.Append("',1,'");
                        strSqlToUpdateBStyleDB.Append(DateTime.Now);
                        strSqlToUpdateBStyleDB.Append("',0)|");

                    }
                    else
                    {
                        strSqlToUpdateBStyleDB.Append("");
                        strSqlToUpdateErpDB.Append("");
                    }
                }
                else
                {
                    strSqlToUpdateBStyleDB.Append("");
                    strSqlToUpdateErpDB.Append("");
                }
            }
            else
            {
                strSqlToUpdateBStyleDB.Append("");
                strSqlToUpdateErpDB.Append("");
            }
        }

        
    }
}
